package com.mxt.moviedemo.service.impl;

import com.mxt.moviedemo.dao.MoviesMapper;
import com.mxt.moviedemo.entity.*;
import com.mxt.moviedemo.service.MoviesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MoviesServiceImpl implements MoviesService {
    @Autowired
    private MoviesMapper moviesMapper;


    private static final Logger logger = LoggerFactory.getLogger(MoviesServiceImpl.class);

    @Override
    public PageResult<Movies> getMoviesByPage(PageRequest pageRequest) {
        List<Movies> movies = moviesMapper.getMoviesByPage(pageRequest);
        // 过滤掉null元素
        List<Movies> nonNullMovies = new ArrayList<>();
        for (Movies movie : movies) {
            if (movie != null) {
                nonNullMovies.add(movie);
            }
        }
        int total = moviesMapper.getTotalMovies();
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, nonNullMovies);
    }

    @Override
    public PageResult<Movies> searchMoviesByKeyword(PageRequest pageRequest, String keyword) {
        List<Movies> movies = moviesMapper.searchMoviesByKeyword(pageRequest, keyword);
        int total = moviesMapper.getTotalMoviesByKeyword(keyword);
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, movies);
    }

//    @Override
//    public Movies getMovieById(int mid) {
//        return moviesMapper.getMovieById(mid);
//    }

    @Override
    public List<Geners> getMovieGeners(int mid) {
        return moviesMapper.getMovieGeners(mid);
    }

//    @Override
//    public List<Actors> getMovieActors(int mid) {
////        System.out.println(mid);
//        return moviesMapper.getMovieActors(mid);
//    }

//    @Override
//    public List<Directors> getMovieDirectors(int mid) {
////        System.out.println(mid);
//        return moviesMapper.getMovieDirectors(mid);
//    }

    @Override
    public Movies getMovieById(int mid) {
        Movies movie = moviesMapper.getMovieById(mid);
        logger.info("根据 ID {} 获取的电影信息: {}", mid, movie);
        return movie;
    }

    @Override
    public List<Actors> getMovieActors(int mid) {
        List<Actors> actors = moviesMapper.getMovieActors(mid);
        logger.info("根据电影 ID {} 获取的演员信息: {}", mid, actors);
        return actors;
    }

    @Override
    public List<Directors> getMovieDirectors(int mid) {
        List<Directors> directors = moviesMapper.getMovieDirectors(mid);
        logger.info("根据电影 ID {} 获取的导演信息: {}", mid, directors);
        return directors;
    }

    @Override
    public PageResult<Geners> getGenersByPage(PageRequest pageRequest) {
        List<Geners> geners = moviesMapper.getGenersByPage(pageRequest);
        // 过滤掉null元素
        List<Geners> nonNullGeners = new ArrayList<>();
        for (Geners gener : geners) {
            if (geners != null) {
                nonNullGeners.add(gener);
            }
        }
        int total = moviesMapper.getTotalGeners();
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, nonNullGeners);
    }

    @Override
    public int addMovie(Movies movie) {
        return moviesMapper.addMovie(movie);
    }

    @Override
    public int deleteMovie(int mid) {
        moviesMapper.deleteMovieActorsByMid(mid);
        moviesMapper.deleteMovieGenersByMid(mid);
        moviesMapper.deleteMovieDirectorsByMid(mid);
        return moviesMapper.deleteMovie(mid);
    }

    @Override
    public int updateMovie(Movies movie) {
        return moviesMapper.updateMovie(movie);
    }

    @Override
    public List<Movies> searchMoviesByName(String mname) {
        return moviesMapper.searchMoviesByName(mname);
    }

    @Override
    public Movies getMovieByName(String mname) {
        return moviesMapper.getMovieByName(mname);
    }

    @Override
    public Geners getGenersById(int gid) {
        return moviesMapper.getGenersById(gid);
    }


    @Override
    public List<Geners> searchGenersByName(String gname) {
        return moviesMapper.searchGenersByName(gname);
    }

    @Override
    public int addGeners(Geners geners) {
        return moviesMapper.addGeners(geners);
    }

    @Override
    public boolean updateGeners(Geners geners) {
        return moviesMapper.updateGeners(geners);
    }

    @Override
    public int deleteGeners(int gid) {
        return moviesMapper.deleteGeners(gid);
    }

}